<template>
  <div class="unpacking-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>拆包</span>
        </div>
      </template>

      <el-form :model="unpackForm" label-width="100px">
        <el-form-item label="包装编号">
          <el-input
            v-model="unpackForm.packageNo"
            placeholder="请扫描或输入包装编号"
            @keyup.enter="handleScan"
          >
            <template #append>
              <el-button @click="handleScan">扫描</el-button>
            </template>
          </el-input>
        </el-form-item>
      </el-form>

      <div v-if="packageInfo" class="package-info">
        <el-descriptions title="包装信息" :column="2" border>
          <el-descriptions-item label="包装类型">{{
            packageInfo.type
          }}</el-descriptions-item>
          <el-descriptions-item label="物料编码">{{
            packageInfo.materialCode
          }}</el-descriptions-item>
          <el-descriptions-item label="包装数量">{{
            packageInfo.quantity
          }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{
            packageInfo.createTime
          }}</el-descriptions-item>
        </el-descriptions>

        <el-table
          :data="packageInfo.items"
          border
          style="width: 100%; margin-top: 20px"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="serialNo" label="序列号" />
          <el-table-column prop="status" label="状态">
            <template #default="scope">
              <el-tag
                :type="scope.row.status === '正常' ? 'success' : 'danger'"
              >
                {{ scope.row.status }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>

        <div class="operation-buttons">
          <el-button type="primary" @click="handleUnpack">确认拆包</el-button>
          <el-button @click="handleClear">清空</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";

const unpackForm = reactive({
  packageNo: "",
});

const packageInfo = ref(null);

const handleScan = () => {
  if (!unpackForm.packageNo) {
    ElMessage.warning("请输入包装编号");
    return;
  }
  // 模拟获取包装信息
  packageInfo.value = {
    type: "箱装",
    materialCode: "M001",
    quantity: 10,
    createTime: "2024-03-20 10:00:00",
    items: [
      { serialNo: "SN001", status: "正常" },
      { serialNo: "SN002", status: "正常" },
      { serialNo: "SN003", status: "正常" },
    ],
  };
};

const handleUnpack = () => {
  ElMessage.success("拆包成功");
  handleClear();
};

const handleClear = () => {
  unpackForm.packageNo = "";
  packageInfo.value = null;
};
</script>

<style scoped>
.unpacking-container {
  padding: 20px;
}

.package-info {
  margin-top: 20px;
}

.operation-buttons {
  margin-top: 20px;
  text-align: right;
}
</style>
